<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>新增装修收款单</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="jquery/jquery-ui-1.8.19.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery/jquery.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui-1.8.19.custom.min.js"></script>
<script src="script/commons.js" type="text/javascript"></script>
<script src="script/calendar.js" type="text/javascript"></script>
<script src="script/objects.js" type="text/javascript"></script>
<script type="text/javascript">
	$(document).ready(function(e) {
        <c:if test="${modifyObject != null}">
			$("#balanceId").val('${modifyObject.balanceId}');
			// 设置渠道 并 获取店铺
			$("#customer").val('${modifyObject.finCustomer.customerid}');
			<c:if test="${modifyObject.shop.isenable}">
				getShops(
					function() {
						$("#shop").val('${modifyObject.shop.customerid}');
					}
				);
			</c:if>
			<c:if test="${!modifyObject.shop.isenable}">
				$("#shop").append(
					$("<option></option>").val("${modifyObject.shop.customerid}").html("${modifyObject.shop.customer}")
				).val("${modifyObject.shop.customerid}").removeAttr("disabled");
			</c:if>
			
			$("#fitmentDate").val('${modifyObject.fitmentDate}');
			$("#shopArea").val('${modifyObject.shopArea}');
			$("#decorationDate").val('${modifyObject.decorationDate}');
			$("#costProperty").val('${modifyObject.costProperty}');
			$("#balancePrice").val('${modifyObject.balancePrice}');
			$("#balanceRemark").val(HTMLDecode(toNewLine('${modifyObject.balanceRemark}')));
			
			// 赋值制单日期
			$("#writeDate").val('${modifyObject.writeDate}');
			$("#wDate").html('${modifyObject.writeDate}');
			// 修改外观。修改导航标题
			$("#modifyTitle").text("修改");
			// 修改隐藏字段表示为修改模式
			$("#isModify").val("1");
			// 单据标号调整为只读
			$("#receiptId").attr("readonly", "readonly");
			// 设置显示的字数长度
			setAlertLength();
		</c:if>
		<c:if test="${modifyObject == null}">
			$("#balanceId").val('${balanceId}');
			$("#isModify").val("0");
			$("#writeDate").val(getCurTime());
			$("#wDate").html(getCurTime());
			getShops();
		</c:if>
    });
	
	function getShops(onSuccess, shopId) {
		var p = $("#customer").val();
		var data = {p : p};
		
		$.post("forwardReceiptadd.do", data,
			function(data, s) {
				$("#shop > option").remove();
				if (data != null && data != ""){
					var shops = eval(data);
					for(var i = 0; i < shops.length; i++){
						$("#shop").append(
							$("<option></option>").val(shops[i].id).html(shops[i].name)
						);
					}
					$("#shop").removeAttr("disabled");
				}else{
					$("#shop").attr("disabled","true");
					//$("#shop").val("-1");
				}
				if (onSuccess != null){
					onSuccess();
				}
			}
		);
	}

	function check() {
		with (document.all) {
			if (customer.value == "") {
				alert("客户名称不允许为空");
				return false;
			}
			if (shopName.value == "") {
				alert("店铺名称不允许为空");
				return false;
			}
			if (balancePrice.value == "") {
				alert("金额不允许为空");
				$("#balancePrice").focus();
				$("#balancePrice").select();
				return false;
			}
			if($("#fitmentDate").val() == "") {
				alert("请选择日期");
				$("#fitmentDate").click();
				return false;
			}
			if($("#writeDate").val() == "") {
				alert("请选择制单日期");
				$("#writeDate").click();
				return false;
			}
			if($("#decorationDate").val() == "") {
				alert("请选择装修日期");
				$("#decorationDate").click();
				return false;
			}
			if (isNaN(balancePrice.value)) {
				alert("金额必须填写数字！");
				$("#balancePrice").focus();
				$("#balancePrice").select();
				return false;
			}
			if (isNaN(shopArea.value)) {
				alert("店铺面积必须填写数字！");
				$("#shopArea").focus();
				$("#shopArea").select();
				return false;
			} 
			if(parseInt($("#alertLength").text()) < 0) {
				alert("备注的字数太多了。。。。只能写500字！");
				return false;
			}
			else {
				$("#balanceRemark").text(HTMLEncode($("#balanceRemark").text()));
				document.finbalanceForm.submit();
			}
		}
	}
</script>
<style type="text/css">
#shop {
	/*visibility: hidden;*/
}
#setWidth {
	width: 100%;
}
</style>
</head>
<body>
	<html:form method="post" action="balanceadd.do">
  		<div id="setWidth">
        <div class="button_bar">
			<c:if
				test="${(finuser.role.roleId == 1 or finuser.role.roleId == 4) or (modifyObject != null and !modifyObject.isClosed)}">
				<input type="button" value="保存" onclick="check()">
			</c:if>
		</div>
		<table class="query_form_table detail_form_table">
			<tr>
				<th colspan="4"><span class="red_star">*</span>为必填项</th>
			</tr>
			<html:hidden property="finUser" value="${finuser.uname}" />
			<tr>
				<th>单据编号</th>
				<td><html:text property="balanceId" styleId="balanceId" readonly="true" /> <span class="red_star">*</span></td>
				<th>渠道名称</th>
				<td>
                	<html:select property="customer" onchange="getShops()" styleId="customer">
          				<html:options collection="customers" property="customerid" labelProperty="customer" />
                	</html:select>
				</td>
			</tr>
			<tr>
				<th>店铺名称</th>
				<td>
                    <html:select property="shopName" styleId="shop" disabled="true">
                    </html:select>
				</td>
				<th>日期</th>
				<td><html:text property="fitmentDate" styleId="fitmentDate"
						onclick="show_cele_date(this,'','',this)" readonly="true" /> <span
					class="red_star">*</span></td>
			</tr>
			<tr>
				<th>店铺面积</th>
				<td><html:text property="shopArea" styleId="shopArea" />&nbsp;m²
				</td>
				<th>制单日期</th>
				<td>
                <span id="wDate"></span>
        		<html:hidden property="writeDate" styleId="writeDate" /></td>
			</tr>

			<tr>
				<th>费用性质</th>
				<td><html:select property="costProperty" styleId="costProperty">
						<html:option value="我方代付">我方代付</html:option>
						<html:option value="对方代付">对方代付</html:option>
					</html:select> <span class="red_star">*</span></td>
				<th>装修日期</th>
				<td><html:text property="decorationDate" styleId="decorationDate" onclick="show_cele_date(this,'','',this)" size="20"
						readonly="true"></html:text> <span
					class="red_star">*</span></td>
			</tr>
            <tr>
            	<th>金额</th>
				<td colspan="3"><html:text property="balancePrice" styleId="balancePrice"></html:text> <span
					class="red_star">*</span></td>
            </tr>
			<tr>
				<th>备注</th>
				<td colspan="3"><html:textarea property="balanceRemark" styleId="balanceRemark" rows="9" cols="60"></html:textarea>
                <span class="alertLength">还可以写&nbsp;<span id="alertLength">500</span>&nbsp;个字</span></td>
			</tr>
		</table>
        </div>
  		<html:hidden property="isModify" styleId="isModify" />
	</html:form>
</body>
</html>